import java.util.PriorityQueue;
import java.util.Scanner;

/**
 * @author vivi
 * @description
 * @date 2024/5/29
 */
public class demo2 {
    public static void main (String[] args) {
        Scanner in = new Scanner(System.in);
        while(in.hasNext()) {
            char[] s = in.next().toCharArray();
            // 1. 统计所有字符的频次
            int[] hash = new int[300];
            for (char ch : s) {
                hash[ch]++;
            }
            // 2. 把所有的频次放⼊堆⾥⾯
            PriorityQueue<Integer> heap = new PriorityQueue<>();
            for (int i = 0; i < 300; i++) {
                if (hash[i] != 0) {
                    heap.offer(hash[i]);
                }
            }
            // 3. 哈夫曼编码
            int ret = 0;
            while (heap.size() > 1) {
                int t1 = heap.poll();
                int t2 = heap.poll();
                ret += t1 + t2;
                heap.offer(t1 + t2);
            }
            System.out.println(ret);
        }
    }
}
